define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');

	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				"class": 'P-creative'
			});

			this.Super('init', arguments);
			this.$param = {};
		},
		afterBuild:function(){
			var self = this;
			var title =  self.getConfig('idType') ? LANG('创意预审(非汇总)'): LANG('创意预审');
			this.append($([
				'<div class="M-formHead">',
					'<label class="M-formHeadTitle">'+ title +'</label>',
					'<span class="fl" pub-name="date" pub-mod="@base/common/date.dateRangeNew"/>',
				'</div>',
				'<div class="cl" pub-name="highGrid" pub-mod="creative/grid/creativePreAudit.base"/>'
			].join('')));

			var endTime = parseInt(Date.now()/1000);
			var beginTime = parseInt(new Date(endTime * 1000 - 6 * 24 * 3600 * 1000) /1000);//默认显示一周内的创意

			var cfg = {
				'date':{
					'pos': 'bR',
					'broadcast':false,
					'userStorage':false,
					'hasNoLimit':true,
					'begin':beginTime,
					'end':endTime,
					'buttons':[
						{
							// 显示的文字
							"text":LANG("今天"),
							"begin":0,
							"end":0,
							"type":"today"
						},
						{
							"text":LANG("昨天"),
							"begin":-1,
							"end":-1,
							"type":"yesterday"
						},
						{
							"text":LANG("最近7天(含今天)"),
							"begin":-6,
							"end":0,
							"type":"latestSeven"
						},
						{
							"text":LANG("最近30天(含今天)"),
							"begin":-29,
							"end":0,
							"type":"latestThirty"
						},
						{
							"text":LANG("本月"),
							"begin":0,
							"end": -(new Date()).getDate(),
							// 特殊指定操作类型
							"type":"currentMonth"
						}
					]
				},
				'highGrid': {
					'hasSearch': false,
					'idType':self.getConfig('idType'),
					'param':{
						'begindate':beginTime,
						'enddate':endTime
					}
				}
			};

			self.buildFromTemplate(cfg);
			self.createAsync('preAuditEdit' + (self.getConfig('idType')|| ''),'creative/popwin/auditEdit.base',{idType:self.getConfig('idType')}, function( mod ) {
				self.$.editPopwin = mod;
			});

			self.createAsync('batchPreAuditEdit' + (self.getConfig('idType')|| ''),'creative/popwin/batchAuditEdit.base', {idType:self.getConfig('idType')},function( mod ) {
				self.$.batchPreAuditEditPopwin = mod;
			});

			var config = this.getConfig();
			self.createAsync('sidebar', '@layout/v1/sidebar.base', {
				'target': config.targetSidebar,
				'items': [
					{'name': 'searchUserName', 'uri':'modules/sideBarBase.search',config:{
						name:'userNameWord',
						searchTip:"账号名称",
						parentClass: 'dib'
					}},
					{'name': 'searchSweety', 'uri':'modules/sideBarBase.search',config:{
						searchTip:"创意包关键字",
						name:'sweetyWord',
						parentClass: 'dib'
					}},

					{'name': 'searchCreative', 'uri':'modules/sideBarBase.search',config:{
						name:'creativeWord',
						searchTip:"创意关键字",
						parentClass: 'dib'
					}},
					{'name': 'creativeType', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('类型：'),
						'data': [
							{id: 0, name: LANG('非代码创意')},
							{id: 4, name: LANG('代码创意')}
						],
						'value': 0
					}},

					//TODO: 加载渠道
					{'name': 'channelId', 'uri':'modules/sideBarBase.channel','config':{'monitorUrl':''}},
					{'name': 'status', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('状态：'),
						'data': [
							{id: 0, name: LANG('所有状态')},
							{id: -2, name: LANG('审核中(蓝)')},
							{id: 1, name: LANG('通过(绿)')},
							{id: -1, name: LANG('拒绝(红)')},
						],
						'value': -2
					}}
				]
			}, function(){});

		},
		// 批量操作-下拉菜单界面
		onBatchShow: function(ev){

			ev.returnValue = [];

			if(pubjs.checkRight('creative.preAudit')){
				ev.returnValue.push({id: 'audit', name: LANG('审核')});
			}
			return false;
		},
		// 批量操作-响应菜单操作
		onBatchSelect: function(ev){
			var data = ev.param[0];
			var ids = ev.param[1];
			var creatives = ev.param[2];
			var key = data.key || '';
			var self = this;

			if(!ids || !ids.length) {
				pubjs.alert(LANG('请先选择创意'));
				return false;
			}
			switch(key){
				case 'audit':
					if(pubjs.checkRight('creative.preAudit')){
						this.$.batchPreAuditEditPopwin.setMode('preAudit').setData({ids:ids,creatives:creatives}).show();
					}
					break;
			}
			return false;
		},
		onSidebarFilter: function(ev){
			var self = this;
			var param = this.$param;

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;
				switch (type) {
					case 'creativeType':
					case 'status':
					case 'channelId':
						param[type] = +id;
						break;
				}
			});

			if(this.get('sidebar/searchUserName')){
				param.userNameWord = this.get('sidebar/searchUserName').getValue();
			}
			if(this.get('sidebar/searchSweety')){
				param.sweetyWord = this.get('sidebar/searchSweety').getValue();
			}
			if(this.get('sidebar/searchCreative')){
				param.creativeWord = this.get('sidebar/searchCreative').getValue();
			}

			this.$.highGrid.reload(param,null ,1);
		},
		onSearch: function(ev){
			this.$param[ev.name] = ev.param || '';
			this.$.highGrid.reload(this.$param,null,1);
			return false;
		},
		reload: function(param){
			param = param || {};
			this.$.highGrid.reload(param);
			return this;
		},
		onOpenAuditPopwin:function(ev){
			this.$.editPopwin.setData(ev.param).show();
			return false;
		},
		onEditSuccess:function(){
			this.$.highGrid.reload();
		},
		setData:function(cost){
			return this;
		},
		editPopwin:function(adExchange){
			var self = this;
			if (self.$.editPopwin) {
				self.$.editPopwin.setData(adExchange).show();
			}
		},
		onInputClick:function(ev){
			if(ev.name == 'addButton'){
				this.$.editPopwin.reset().show();
			}
		},
		onAuditSuccess: function(ev){
			if(this.$.highGrid){
				this.$.highGrid.setSelectRowIds([]);
				this.reload();
			}
		},
		onDateRangeChange:function (ev) {
			var param = ev.param;
			if(param.nowTimestamp){
				var timeStamp = param.nowTimestamp;
				this.$.highGrid.reload({'begindate':timeStamp.begindate,'enddate':timeStamp.enddate},null,1);
			}
			return false;
		}
	});

	exports.main = List;
});